using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    public class DMRelationshipPropertyTypesAreSame : IBusinessObjectRule
    {
        private DMRelationshipProperty _relProp;

//        public DMRelationshipPropertyTypesAreSame(DMRelationshipProperty relationshipProp)
//        {
//            if (relationshipProp == null) throw new ArgumentNullException("relationshipProp");
//            _relProp = relationshipProp;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _relProp = (DMRelationshipProperty) bo;
            //This should be validated by another rule
            if (_relProp.RelatedProperty == null || _relProp.OwnerProperty == null) return true;
            //This should be validated by another rule
            if ((_relProp.RelatedProperty.PropertyType == null) || (_relProp.OwnerProperty.PropertyType == null))
            {
                return true;
            }
            return _relProp.AreBothPropTypesSame();
        }

        public string Name
        {
            get { return "DMRelationshipPropertyTypesAreSame"; }
        }

        public string Message
        {
            get
            {
                IDMRelationshipProperty reverseRelationshipProperty = _relProp.ReverseRelationshipProperty;
                IDMProperty dmProperty = _relProp.Property;
                return string.Format("The Relationship Property '{0}' property type('{1}') is not the same as its Related Relationship Property " +
                                     "'{2}' property type('{3}')",
                                     _relProp, dmProperty.PropertyTypeString, reverseRelationshipProperty, reverseRelationshipProperty.Property.PropertyTypeString);
            }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}